package com.student.liu.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.student.liu.entity.Cook;
import com.student.liu.exception.ServiceValidationException;
import com.student.liu.mapper.ICookMapper;
import com.student.liu.service.ICookService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Map;

/**
 * 用户服务接口 实现类
 */
@Slf4j
@Service
public class CookServiceImpl extends ServiceImpl<ICookMapper, Cook> implements ICookService {

    @Resource
    ICookMapper cookMapper;

    @Override
    public Integer login(Cook cook) {
        //1.判断user是否存在
        QueryWrapper userQueryWrapper = new QueryWrapper();
        userQueryWrapper.eq("username", cook.getUsername());
        if (count(userQueryWrapper) == 0) {
            //未找到该用户
            throw new ServiceValidationException("登录失败，账号不存在，请前往注册", 401);
        }
        //2.密码判断
        QueryWrapper<Cook> loginUserQueryWrapper = new QueryWrapper();
        loginUserQueryWrapper.select("id", "usable");
        loginUserQueryWrapper.eq("username", cook.getUsername());
        loginUserQueryWrapper.apply("password=MD5({0})", cook.getUsername() + cook.getPassword());
        Map<String, Object> map = getMap(loginUserQueryWrapper);
        log.info("map:" + map);
        if (map == null) {
            //检测到用户，但是密码错误
            throw new ServiceValidationException("登录失败，账号或密码错误", 401);
        }
        if (!Boolean.parseBoolean(map.get("usable").toString())) {
            //检测到用户，但是账号被禁用了
            throw new ServiceValidationException("登录失败，账号被禁用", 401);
        }
        return Integer.parseInt(map.get("id").toString());
    }



    @Override
    public void register(Cook cook) {
        //1.先判断用户名是否存在
        QueryWrapper<Cook> queryWrapper = new QueryWrapper();
        queryWrapper.eq("username", cook.getUsername());
        if (count(queryWrapper) > 0) {
            throw new ServiceValidationException("用户名已存在", 402);
        }
        //新增
        cook.setUsable(true);
        if (cookMapper.insertCook(cook) == 0) {
            throw new ServiceValidationException("注册失败，请联系客服", 402);
        }
        log.info("新注册用户的ID：" + cook.getId());
    }

    @Override
    public void Change(String username,String password) {
        Cook cook = new Cook();
        cook.setUsername(username);
        cook.setPassword(password);
        if (cookMapper.updateCook(cook) == 0) {
            throw new ServiceValidationException("修改失败，请联系客服", 402);
        }
    }


    @Override
    public Integer determine(String username,String password) {
        Cook cook = new Cook();
        cook.setUsername(username);
        cook.setPassword(password);
        return cookMapper.getByIdSelectPassword(cook);
    }

    @Override
    public void disableCook(Integer cookId) {
        Cook cook = new Cook();
        cook.setId(cookId);
        cook.setUsable(false);
        updateById(cook);
    }

    @Override
    public void ableCook(Integer cookId) {
        Cook cook = new Cook();
        cook.setId(cookId);
        cook.setUsable(true);
        updateById(cook);
    }


    @Override
    public IPage<Map<String, Object>> cookPage(Integer pageIndex, Integer pageSize, String name) {
        Page page = new Page(pageIndex, pageSize);
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.select("cook.*");
        queryWrapper.like(name!=null,"nickname",name);
        log.info(queryWrapper.toString());
        return pageMaps(page, queryWrapper);
    }

    /**
     * 根据id获取信息
     * @param id
     * @return
     */
    @Override
    public Cook cookGetById(Integer id) {
        Cook cook = getById(id);
        if (cook == null) {
            throw new ServiceValidationException("未找到对应厨师信息，请核实传递的ID", 402);
        }
        return cook;
    }

}
